package com.phantasy.utils
{
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;

	public class ObjectUtils
	{
		private static var attributes:Dictionary = new Dictionary();
		
		public function ObjectUtils()
		{
		}
		
		public static function copy(_arg1:Object):Object
		{
			var _local2:ByteArray = new ByteArray();
			_local2.writeObject(_arg1);
			_local2.position = 0;
			var _local3:Object = _local2.readObject();
			return (_local3);
		}
		
		public static function matchArray(_arg1:Array, _arg2:Array):Boolean
		{
			var _local3:*;
			var _local4:*;
			var _local5:String;
			var _local6:String;
			if (_arg1.length != _arg2.length)
			{
				return (false);
			};
			var _local7:int;
			while (_local7 < _arg1.length)
			{
				_local3 = _arg1[_local7];
				_local4 = _arg2[_local7];
				_local5 = getQualifiedClassName(_local3);
				_local6 = getQualifiedClassName(_local4);
				if (_local5 != _local6)
				{
					return (false);
				};
				if ((((((((((_local3 is int)) || ((_local3 is String)))) || ((_local3 is Number)))) || ((_local3 is Boolean)))) || ((_local3 is uint))))
				{
					if (_local3 != _local4)
					{
						return (false);
					};
				}
				else
				{
					if ((_local3 is Array))
					{
						if (!matchArray(_local3, _local4))
						{
							return (false);
						};
					}
					else
					{
						if (!matchNotDynamic(_local3, _local4, Class(ApplicationDomain.currentDomain.getDefinition(_local5))))
						{
							return (false);
						};
					};
				};
				_local7++;
			};
			return (true);
		}
		
		public static function matchNotDynamic(source, target, type:Class):Boolean
		{
			var variableNames:Vector.<Object>;
			var xml:XML;
			var variableList:XMLList;
			var variableLength:int;
			var i:int;
			var sourceItem:* = undefined;
			var targetItem:* = undefined;
			var variableName:String;
			var variableType:String;
			var j:int;
			if ((((source == null)) && ((target == null))))
			{
				return (true);
			};
			if (!attributes.hasOwnProperty(type))
			{
				try
				{
					xml = describeType(new (type)());
					variableList = xml.variable;
					variableLength = variableList.length();
					variableNames = new Vector.<Object>();
					i = 0;
					while (i < variableLength)
					{
						variableNames.push({
							"name":variableList[i].@name,
							"type":variableList[i].@type
						});
						i = (i + 1);
					};
					attributes[type] = variableNames;
				}
				catch(e:Error)
				{
				};
			}
			else
			{
				variableNames = attributes[type];
			};
			if (((!((source == null))) && (!((target == null)))))
			{
				j = 0;
				while (j < variableNames.length)
				{
					variableName = variableNames[j]["name"];
					variableType = variableNames[j]["type"];
					sourceItem = source[variableName];
					targetItem = target[variableName];
					if ((((((((((((sourceItem is int)) || ((sourceItem is String)))) || ((sourceItem is Number)))) || ((sourceItem is Boolean)))) || ((sourceItem is Boolean)))) || ((sourceItem is uint))))
					{
						if (sourceItem != targetItem)
						{
							return (false);
						};
					}
					else
					{
						if ((sourceItem is Array))
						{
							if (!matchArray(sourceItem, targetItem))
							{
								return (false);
							};
						}
						else
						{
							if (!matchNotDynamic(sourceItem, target[variableName], Class(ApplicationDomain.currentDomain.getDefinition(variableType))))
							{
								return (false);
							};
						};
					};
					j = (j + 1);
				};
				return (true);
			};
			return (false);
		}
	}
}